package com.sinosoft.dsp.platform.components;

import java.util.HashMap;
import java.util.Map;

import org.primefaces.json.JSONObject;

import com.sinosoft.builder.templates.view.LayoutType;
import com.sinosoft.builder.templates.view.SceneType;
import com.sinosoft.builder.templates.view.TemplateType;
import com.sinosoft.dsp.platform.services.PlatformViewService;
import com.sinosoft.dsp.platform.utils.SinoManagedBean;
import com.sinosoft.dsp.system.core.exception.SinoException;
import com.sinosoft.dsp.system.core.exception.SinoXMLException;
import com.sinosoft.dsp.system.to.IGenericTO;
import com.sinosoft.dsp.system.utils.Const;

/**
 * ViewBean的抽象类
 * 
 * @since 1.0 2010-4-22
 * @author 潘巍（Peter Pan）
 * 
 */
public class AbstractViewBean implements IViewBean {

	/**
	 * ViewBean的ID
	 */
	private String id;

	/**
	 * 场景ID
	 */
	private String sceneId;

	/**
	 * 视图内容ID
	 */
	private IViewBean content;

	/**
	 * 视图父内容ID
	 */
	private IViewBean parent;

	/**
	 * 场景层
	 */
	private String layoutId;

	/**
	 * 场景模板
	 */
	private String templateId;

	/**
	 * 初始化的参数
	 */
	private JSONObject initParameters;

	/**
	 * ViewBean属性集
	 */
	private Map<String, String> attrs;

	/**
	 * 通用的构造方法，用于初始化基本参数。
	 * 
	 * @param scene
	 * @param layout
	 */
	@Override
	public void construct(String scene, String layout) throws SinoException {
		this.sceneId = scene;
		this.layoutId = layout;
	}

	@Override
	public boolean hasContent() {
		if (this.content == null) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public void setContent(IViewBean vb) {
		this.content = vb;
	}

	@Override
	public IViewBean getContent() {
		return content;
	}

	@Override
	public boolean hasParent() {
		if (this.parent == null) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public void setParent(IViewBean vb) {
		this.parent = vb;
	}

	@Override
	public IViewBean getParent() {
		return this.parent;
	}

	@Override
	public String getId() {
		return id;
	}

	@Override
	public void setId(String id) {
		this.id = id;
	}

	@Override
	public JSONObject getInitParameters() {
		return initParameters;
	}

	@Override
	public void setInitParameters(JSONObject initParameters) {
		this.initParameters = initParameters;
	}

	@Override
	public SceneType getScene() {
		PlatformViewService view = SinoManagedBean
				.findBean("platformViewService");
		return view.getSceneById(this.sceneId);
	}

	@Override
	public LayoutType getLayout() throws SinoXMLException {
		PlatformViewService view = SinoManagedBean
				.findBean("platformViewService");
		return view.getLayout(sceneId, layoutId);
	}

	@Override
	public void setTemplate(String templateId) throws SinoXMLException {
		if (Const.isEmpty(templateId)) {
			return;
		}
		if (!Const.isEmpty(layoutId)) {
			for (TemplateType t : getLayout().getTemplate()) {
				if (templateId.equals(t.getId())) {
					this.templateId = templateId;
					return;
				}
			}
		}
	}

	@Override
	public TemplateType getTemplate() throws SinoXMLException {
		PlatformViewService view = SinoManagedBean
				.findBean("platformViewService");
		return view.getTemplateByLayoutAndId(getLayout(), templateId);
	}

	@Override
	public Map<String, String> getAttrs() {
		return attrs;
	}

	@Override
	public void setAttrs(Map<String, String> attrs) {
		this.attrs = attrs;
	}

	@Override
	public void putAttr(String key, String attr) {
		if (this.attrs == null)
			this.attrs = new HashMap<String, String>();
		if (!Const.isEmpty(key))
			this.attrs.put(key, attr);
	}

	@Override
	public String getAttr(String key) {
		if (this.attrs != null)
			return this.attrs.get(key);
		return null;
	}

	/**
	 * 通过tos初始化ViewBean。<br>
	 * 该方法在通用构造方法construct之后执行。
	 * 
	 * @param tos
	 * @throws SinoException
	 */
	@Override
	public void init(IGenericTO tos) throws SinoException {
	}

	@Override
	public void destroy() {
		if (this.content != null) {
			this.content.destroy();
		}
	}

}
